package form.ui {
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.utils.setInterval;
	
	import form.dispatchers.FormElementDispatcher;
	import form.events.FormElementChangeEvent;
	import form.events.FormElementEvent;
	import form.interfaces.IFormElement;
	import form.ui.component.Input.InputOutline;
	import form.ui.core.FMSprite;
	
	public class Input extends FMSprite implements IFormElement {
		public static const BACKGROUND_COLOR:int = 0xffffff;
		
		public static const FONT_COLOR:int = 0x333333;
		public static const DISABLED_FONT_COLOR:int = 0x999999;
		
		public static const FONT_FAMILY:String = "Verdana";
		public static const FONT_SIZE:int = 12;
		
		public static const TEXT_INDENT:int = 3;
		
		public static const DEFAULT_WIDTH:int = 140;
		public static const DEFAULT_HEIGHT:int = 20;
		
		private static const OUTLINE_MARGIN_LEFT:int = 1;
		private static const OUTLINE_MARGIN_RIGHT:int = 1;
		
		private static const COMPENSATORY_WIDTH:int = 3;
		
		protected var textField:TextField = new TextField();
		protected var outline:InputOutline = new InputOutline();
		
		private var _formName:String;
		
		private var _disabled:Boolean;
		
		private var prompted:Boolean = false;
		
		public function Input() {
			super();
			
			Input.defineInput(this);
			Input.defineTextField(this.textField);
			
			this.textField.addEventListener(FocusEvent.FOCUS_IN, this.onFocusIn);
			this.textField.addEventListener(FocusEvent.FOCUS_OUT, this.onFocusOut);
			
			this.textField.addEventListener(TextEvent.TEXT_INPUT, this.onTextInput);
			this.textField.addEventListener(Event.CHANGE, this.onChange);
			
			this.addChild(this.textField);
			this.addChild(this.outline);
			
			this.textField.x = -Input.OUTLINE_MARGIN_LEFT;
			
			this.width = Input.DEFAULT_WIDTH;
			this.height = Input.DEFAULT_HEIGHT;
			
			this.enable();
			
			var e:FormElementEvent = new FormElementEvent(FormElementEvent.INITIALIZED);
			e.element = this;
			
			FormElementDispatcher.instance.dispatchEvent(e);
		}
		
		public function set formName(value:String):void {
			this._formName = value;
		}
		
		public function get formName():String {
			return this._formName;
		}
		
		public function set formValue(value:String):void {
			var change:Boolean = value != this.formValue;
			
			this.textField.text = value;	
			if (change) { this.onChange(); }
		}
		
		public function get formValue():String {
			return this.textField.text;
		}
		
		public override function set width(value:Number):void {
			super.width = value;
			this.textField.width = this.width + Input.COMPENSATORY_WIDTH;
			this.outline.width = this.width - (Input.OUTLINE_MARGIN_LEFT + Input.OUTLINE_MARGIN_RIGHT) + Input.COMPENSATORY_WIDTH;
		}
		
		public override function set height(value:Number):void {
			super.height = value;
			this.textField.height = this.height;
			this.outline.height = this.height;
		}
		
		public function enable():void {	
			this.textField.textColor = Input.FONT_COLOR;
			
			this.textField.selectable = true;
			this.textField.type = TextFieldType.INPUT;
			
			this.outline.showBlur();
			
			this._disabled = false;
		}
		
		public function disable():void {
			this.textField.textColor = Input.DISABLED_FONT_COLOR;
			
			this.textField.selectable = false;
			this.textField.type = TextFieldType.DYNAMIC;
			
			this.outline.showDisabled();
			
			this._disabled = true;
		}
		
		protected function get disabled():Boolean {
			return this._disabled;
		}
		
		public function set maxLength(value:Number):void {
			this.textField.maxChars = value;
		}
		
		public function get maxLength():Number {
			return this.textField.maxChars;
		}
		
		public function set prompt(value:String):void {
			this.formValue = "";
			this.prompted = true;
			this.textField.text = value;
		}
		
		protected static function defineInput(input:Input):void {
			input.fillColor = Input.BACKGROUND_COLOR;
			input.fillOpacity = 1;
			
			input.strokeThickness = 0;
			input.strokeOpacity = 0;
		}
		
		protected static function defineTextField(textField:TextField):void {
			textField.defaultTextFormat = Input.textFormat;
		}

		protected static function get textFormat():TextFormat {
			var format:TextFormat = new TextFormat();
			
			format.font = Input.FONT_FAMILY;
			format.size = Input.FONT_SIZE;
			format.indent = Input.TEXT_INDENT;
			
			return format;
		}
		
		private function onFocusIn(e:Event):void {
			if (!this.disabled) {
				outline.showFocus();
			}
		}
		
		private function onFocusOut(e:Event):void {
			if (!this.disabled) {
				outline.showBlur();
			}
		}
		
		private function onTextInput(e:TextEvent):void {
			if (this.prompted) {
				this.formValue = "";
				this.prompted = false;
			}
		}
		
		private function onChange(event:Event = null):void {
			var e:FormElementChangeEvent = new FormElementChangeEvent(FormElementChangeEvent.INPUT_VALUE_CHANGED);
			e.element = this;
				
			this.dispatchEvent(e);
		}
	}
}